﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace pswmanage
{
    class Search
    {
        public DataTable SearchItem(string key, string fuse, string fserver, string fsystem, string username, string fnote)
        {
            DataTable dt = new DataTable("bindbox");
            SqlConnection conn = new SqlConnection(Common.ConnString());
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter adp = new SqlDataAdapter();
            try
            {

                string sql = "dbo.getpsw @key,@fuse,@fserver,@fsystem,@username,@fnote";


                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@key", key);
                cmd.Parameters.AddWithValue("@fuse", fuse);
                cmd.Parameters.AddWithValue("@fserver", fserver);
                cmd.Parameters.AddWithValue("@fsystem", fsystem);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@fnote", fnote);
                adp.SelectCommand = cmd;
                adp.Fill(dt);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show("异常："+ e.ToString());
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }


        internal int DelPSW(string key, string fuse, string fserver, string fsystem, string username)
        {
            SqlConnection conn = new SqlConnection(Common.ConnString());
            SqlCommand cmd = new SqlCommand();
            SqlTransaction tran = null;
            try
            {
                string sql = "dbo.delpsw @key,@fuse,@fserver,@fsystem,@username";
                conn.Open();
                tran = conn.BeginTransaction();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@key", key);
                cmd.Parameters.AddWithValue("@fuse", fuse);
                cmd.Parameters.AddWithValue("@fserver", fserver);
                cmd.Parameters.AddWithValue("@fsystem", fsystem);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Transaction = tran;
                cmd.ExecuteNonQuery();
                tran.Commit();
                return 1;

            }
            catch //(Exception e)
            {
                tran.Rollback();
                return -1;
            }
            finally
            {
                conn.Close();
            }
        }


        internal int ChgPSW(string key, string newkey, string fuse, string fserver, string fsystem, string username)
        {
            SqlConnection conn = new SqlConnection(Common.ConnString());
            SqlCommand cmd = new SqlCommand();
            SqlTransaction tran = null;
            try
            {
                string sql = "dbo.chgpswkey @key,@newkey,@fuse,@fserver,@fsystem,@username";
                conn.Open();
                tran = conn.BeginTransaction();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@key", key);
                cmd.Parameters.AddWithValue("@newkey", newkey);
                cmd.Parameters.AddWithValue("@fuse", fuse);
                cmd.Parameters.AddWithValue("@fserver", fserver);
                cmd.Parameters.AddWithValue("@fsystem", fsystem);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Transaction = tran;
                cmd.ExecuteNonQuery();
                tran.Commit();
                return 1;

            }
            catch //(Exception e)
            {
                tran.Rollback();
                return -1;
            }
            finally
            {
                conn.Close();
            }
        }



        internal int CopyPSW(string key, string newkey, string fnewuse, string fuse, string fserver, string fsystem, string username)
        {
            SqlConnection conn = new SqlConnection(Common.ConnString());
            SqlCommand cmd = new SqlCommand();
            SqlTransaction tran = null;
            try
            {
                string sql = "dbo.copypsw @key,@newkey,@fuse,@fnewuse,@fserver,@fsystem,@username";
                conn.Open();
                tran = conn.BeginTransaction();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@key", key);
                cmd.Parameters.AddWithValue("@newkey", newkey);
                cmd.Parameters.AddWithValue("@fuse", fuse);
                cmd.Parameters.AddWithValue("@fnewuse", fnewuse);
                cmd.Parameters.AddWithValue("@fserver", fserver);
                cmd.Parameters.AddWithValue("@fsystem", fsystem);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Transaction = tran;
                cmd.ExecuteNonQuery();
                tran.Commit();
                return 1;

            }
            catch //(Exception e)
            {
                tran.Rollback();
                return -1;
            }
            finally
            {
                conn.Close();
            }
        }

        internal int AddPSW(string key, string fuse, string fserver, string fsystem, string username, string password, string fnote)
        {
            SqlConnection conn = new SqlConnection(Common.ConnString());
            SqlCommand cmd = new SqlCommand();
            SqlTransaction tran = null;
            try
            {
                string sql = "dbo.addpsw @key,@fuse,@fserver,@fsystem,@username,@password,@fnote";
                conn.Open();
                tran = conn.BeginTransaction();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue("@key", key);
                cmd.Parameters.AddWithValue("@fuse", fuse);
                cmd.Parameters.AddWithValue("@fserver", fserver);
                cmd.Parameters.AddWithValue("@fsystem", fsystem);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                cmd.Parameters.AddWithValue("@fnote", fnote);
                cmd.Transaction = tran;
                cmd.ExecuteNonQuery();
                tran.Commit();
                return 1;
            }
            catch //(Exception e)
            {
                tran.Rollback();
                return -1;
            }
            finally
            {
                conn.Close();
            }
        }

    }
}
